﻿/**
* @description  Class to handle rendering of individual graphic elements within a UI widget.
*/
package com.foed {

	import flash.display.DisplayObject;
	import flash.utils.getQualifiedClassName;

	import com.foed.graphic.skins.ISkin;
	import com.foed.graphic.StyleFormat;
	import com.foed.UIObject;
	
	public class Block extends UIObject {
	
		static public const CLASS:String = getQualifiedClassName(Block);
		static public const GRAPHIC:String = "graphic";

		private var _skin:ISkin;
		private var _state:String;

		/**
		* @description	Initializes component properties.
		*/	
		override protected function init():void {
			super.init();
			_classes.push(Block.CLASS);
		}
	
		/**
		* @description	Creates all necessary nested graphics and components.
		*/	
		override protected function draw():void {
			var pSkin:Class = getSkin(GRAPHIC);
			_skin = addChild(DisplayObject(new pSkin())) as ISkin;
			setSize(width, height);
			changeState(_state);
			setStyleFormat(getStyleFormat());
		}

		/**
		* @description  Changes the frame or calls changeState() in the loaded or drawn skin.
		*
		* @param  pState  The state to change the skin to. Should be constant of SkinStates. 
		*/
		private function changeState(pState:String):void {
			_state = pState;
			if (!_initialized) return;
			_skin.changeState(_state);
		}
	
		/**
		* @description	Sets the width and height of component.
		*
		* @param  pWidth  Width of component.
		* @param  pHeight  Height of component.
		*/	
		override public function setSize(pWidth:Number, pHeight:Number):void {
			super.setSize(pWidth, pHeight);
			if (!_initialized) return;
			_skin.setSize(width, height);
		}

		/**
		* @description	Sets the styleFormat instance to apply to component.
		*
		* @param  pFormat  Instance of StyleFormat to apply to component.
		*/
		override public function setStyleFormat(pStyleFormat:StyleFormat):void {
			super.setStyleFormat(pStyleFormat);
			if (!_initialized) return;
			_skin.styleElements(getStyleFormat());
		}
	
		/**
		* @description	Returns the current state of the block, which determines the current drawing of the skin.
		*
		* @returns  The skin state as a string.
		*/
		public function get state():String {
			return _state;
		}
	
		/**
		* @description	Sets the current state of the block, which determines the current drawing of the skin.
		*
		* @param  pState  The skin state to change to as a string.
		*/
		public function set state(pState:String):void {
			changeState(pState);
		}
	
	}

}